package com.xjoyt.pay.master.core.order.handle;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.gateway.params.OrderParam;
import com.xjoyt.pay.master.dto.OrderUrlResult;
import com.xjoyt.pay.master.repository.OrderRepository;
import com.xjoyt.pay.vo.PayType;

public abstract class AbstractOrderCreateHandle {

	protected static final Logger logger = LoggerFactory.getLogger(AbstractOrderCreateHandle.class);

	@Autowired
	protected OrderRepository repository;

	private AbstractOrderCreateHandle next;

	public void setNext(AbstractOrderCreateHandle next) {
		this.next = next;
	}

	public AbstractOrderCreateHandle getNext() {
		return this.next;
	}

	public abstract ExecuteResult<OrderUrlResult> handle(OrderParam param, PayType payType,
			Map<String, Object> args);

	@SuppressWarnings("unchecked")
	protected <T> T getObject(Class<T> cls, Map<String, Object> args) {
		String key = cls.getSimpleName();
		if (!args.containsKey(key)) {
			return null;
		}
		Object value = args.get(key);
		return (T) value;
	}

	protected <T> void putObject(Object obj, Map<String, Object> args) {
		if (obj == null) {
			return;
		}
		Class<?> cls = obj.getClass();
		String key = cls.getSimpleName();
		if (args.containsKey(key)) {
			args.remove(key);
		}

		args.put(key, obj);
	}
}
